import soket_server
import camera
import jyzk_io
#import ocr

from concurrent.futures import ThreadPoolExecutor

# 数据通信格式：
# 字节序：小端字节序
# 包格式：包头+包长+包ID+数据+包尾
# 包头："PACKET_START"
# 包长：8字节无符号整数（字节数，包含[包类型 + 数据 + 包尾]）
# 包ID：4字节char
# 数据：任意字节
# 包尾："PACKET_END"


# //Socket连接参数
# #define NET_TCP_PORT             8888    //TCP服务器监听“起始”端口号
# #define NET_PORT_SPACE           10      //尝试监听、绑定、广播端口号间隔
#
# #define NET_SOCKET_TIMEOUT       10*1000 //多长时间没有收到客户端信息说明已经断开
# #define NET_SHAKE_HAND_ENABLED   false
#
# //包数据结构 : [ 包头 + 长度(8字节) + 包类型(4字节) + 数据 + 包尾 ]
# #define NET_PACKET_START                "PACKET_START"
# #define NET_PACKET_END                  "PACKET_END"
# #define NET_PACKET_TYPE_BYTES           4    //包类型占几个字节
# #define NET_PACKET_LTNGTH_BYTES         8    //包数据长度占几个字节
#
#  //包类型
# #define NET_PACKET_TYPE_CMD                 "0001" //命令
# #define NET_PACKET_TYPE_LOG				  "0002" //日志
# #define NET_PACKET_TYPE_IMAGE               "0003" //图像
# #define NET_PACKET_TYPE_SYSTEM_STATUS       "0004" //系统状态
#
# //命令枚举（即发送命令时具体填入的数据）
# // 客户端命令
# #define REQUEST_OPEN_CAMERA              "OPEN_CAMERA" //打开相机
# #define REQUEST_CLOSE_CAMERA             "CLOSE_CAMERA" //关闭相机
# #define REQUEST_TAKE_PHOTO               "TAKE_PHOTO" //拍照
# // 服务端回复命令
# #define RESPONSE_CAMERA_OPEN_SUCCESS            "CAMERA_OPEN_SUCCESS" //相机打开成功
# #define RESPONSE_CAMERA_OPEN_FAILED             "CAMERA_OPEN_FAILED" //相机打开失败
# #define RESPONSE_CAMERA_CLOSE_SUCCESS           "CAMERA_CLOSE_SUCCESS" //相机关闭成功
# #define RESPONSE_CAMERA_CLOSE_FAILED            "CAMERA_CLOSE_FAILED" //相机关闭失败
# #define RESPONSE_PHOTO_TAKEN_SUCCESS            "PHOTO_TAKEN_SUCCESS" //照片拍摄成功
# #define RESPONSE_PHOTO_TAKEN_FAILED             "PHOTO_TAKEN_FAILED" //照片拍摄失败

class MainApp:
    server = None
    camera = None
    pool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")
    ocr = None
    def __init__(self):
        self.server = soket_server.SocketServer('127.0.0.1', 8888)
        self.camera = camera.GxCamera()
        # self.ocr = ocr.OcrServer()
        # 注册接收到包的回调函数

        # 注册命令包的回调函数
        self.server.register_recv_callback(b"0001", self.recv_cmd_pack_callback)

        # 注册日志包的回调函数
        self.server.register_recv_callback(b"0002", self.recv_log_pack_callback)

        # 注册图像包的回调函数
        self.server.register_recv_callback(b"0003", self.recv_image_pack_callback)

        # 注册系统状态包的回调函数
        self.server.register_recv_callback(b"0004", self.recv_system_status_pack_callback)

    def __del__(self):
        self.server.stop()
        self.camera.close_camera()


    def run(self):
        self.server.start()

    def task_open_camera(self):
        # 打开相机
        print("开始打开相机")
        if self.camera.open_camera() is None:
            print("打开相机失败")
            self.server.send_data(b"0001", b"CAMERA_OPEN_FAILED")
        else:
            print("打开相机成功")
            self.server.send_data(b"0001", b"CAMERA_OPEN_SUCCESS")

    def task_close_camera(self):
        # 关闭相机
        self.camera.close_camera()
        # 发送回应包
        self.server.send_data(b"0001", b"CAMERA_CLOSE_SUCCESS")

    def task_take_photo(self):
        # 拍照
        qimage = self.camera.get_image()
        # 发送回应包
        if qimage is None:
            self.server.send_data(b"0001", b"PHOTO_TAKEN_FAILED")
        else:
            # self.server.send_data(b"0001", b"PHOTO_TAKEN_SUCCESS")
            image_bytes = jyzk_io.qimage_to_bytes(qimage, 'PNG')
            self.server.send_data(b"0003", jyzk_io.qimage_to_bytes(qimage))

    def recv_cmd_pack_callback(self, data):
        # 解析命令包
        if data == b"OPEN_CAMERA":
            print("收到打开相机命令")
            # ret = self.pool.run(self.task_open_camera, (self,), callback=None)
            future = self.pool.submit(self.task_open_camera)
        elif data == b"CLOSE_CAMERA":
            print("收到关闭相机命令")
            # ret = self.pool.run(self.task_close_camera, (self,), callback=None)
            future = self.pool.submit(self.task_close_camera)
        elif data == b"TAKE_PHOTO":
            print("收到拍照命令")
            # ret = self.pool.run(self.task_take_photo, (self,), callback=None)
            future = self.pool.submit(self.task_take_photo)
        else:
            print("无效的命令包") 
    
    def recv_log_pack_callback(self, data):
        # 解析日志包
        print(data)
    
    def recv_image_pack_callback(self, data):
        # 解析图像包
        print("无效的包类型：图像包")
    
    def recv_system_status_pack_callback(self, data):
        # 解析系统状态包
        print("无效的包类型：系统状态包")

    #     self.server.register_recv_callback(1, self.recv_register_pack_callback)
    #     self.server.register_recv_callback(3, self.recv_heartbeat_pack_callback)
    #     self.server.register_recv_callback(5, self.recv_open_camera_pack_callback)
    #     self.server.register_recv_callback(8, self.recv_close_camera_pack_callback)
    #     self.server.register_recv_callback(11, self.recv_take_photo_pack_callback)
    #     self.server.register_recv_callback(14, self.recv_ocr_pack_callback)

    # def run(self):
    #     self.server.start()

    # def recv_register_pack_callback(self, data):
    #     # 发送回应包
    #     pack_data = comcs.pack_response(2, 1)
    #     self.server.send_data(pack_data)

    # def recv_heartbeat_pack_callback(self, data):
    #     # 发送回应包
    #     pack_data = comcs.pack_response(4, 1)
    #     self.server.send_data(pack_data)

    # def recv_open_camera_pack_callback(self, data):
    #     # 发送请求回应包
    #     pack_data = comcs.pack_response(6, 1)
    #     self.server.send_data(pack_data)

    # def recv_close_camera_pack_callback(self, data):
    #     # 发送请求回应包
    #     pack_data = comcs.pack_response(9, 1)
    #     self.server.send_data(pack_data)

    # def recv_take_photo_pack_callback(self, data):
    #     # 发送请求回应包
    #     pack_data = comcs.pack_response(12, 1)
    #     self.server.send_data(pack_data)

    # def recv_ocr_pack_callback(self, data):
    #     # 发送请求回应包
    #     pack_data = comcs.pack_response(15, 1)
    #     self.server.send_data(pack_data)